package model;

/**
 * This class provide a support to know max used space by a function
 * @author Thomas DONZELLE
 */
public class MemoryThread extends Thread{
	public boolean run = true;
	private long maxUsedMem = 0;
	private long baseUsedMem=0;
	private long currentUsage=0;
	private Runtime runtime = Runtime.getRuntime();
	
	public void run(){
		while(run){
			currentUsage=(runtime.totalMemory()-runtime.freeMemory())-baseUsedMem;
			setMaxUsedMem(Math.max(getMaxUsedMem(),currentUsage));
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
	public synchronized void setMaxUsedMem(long mUM){	
		maxUsedMem = mUM;
	}
	public synchronized void reset(){
		baseUsedMem=runtime.totalMemory() - runtime.freeMemory();
		System.gc();
		setMaxUsedMem(0);
	}
	public synchronized long getMaxUsedMem(){
		return maxUsedMem;
	}
}
